﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using TechCraftEngine;
using TechCraftEngine.Common;
using Blox.States;
using System.Runtime.InteropServices;
//using fbDeprofiler;

namespace Blox
{
    public class Game : TechCraftGame
    {
        //public static Global Global = new Global() { };

        public Game() {
            //DeProfiler.Run(); 
            FrameRateCounter frameRate = new FrameRateCounter(this);
            frameRate.DrawOrder = 1;
            Components.Add(frameRate);
            VersionTxt t = new VersionTxt(this);
            t.DrawOrder = 1;
            Components.Add(t);

        }

        protected override void Initialize()
        {
            TitleState _state = new TitleState();
            _state.Game = this;
            _state.Initialize();
            _state.LoadContent();
            StateManager.ActiveState = _state;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        /*[DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
        public static extern IntPtr SetFocus(HandleRef hWnd);

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);*/

        protected override void OnDeactivated(object sender, EventArgs args)
        {
            if (Global.tempbrkblock)
            {
                //SetForegroundWindow(this.Window.Handle);
                return;
            }
            else if(StateManager.ActiveState == StateManager.GetState(typeof(PlayingState)))
            {
                IntPtr hwnd = this.Window.Handle;
                this.OnActivated(sender, args);
                

                /*if (IsIconic(hwnd))
                {
                    ShowWindowAsync(hwnd, 9);
                }

                SetForegroundWindow(hwnd);
                SetFocus(new HandleRef(null, this.Window.Handle));*/
                return;
            }
            else
            {
                StateManager.ActiveState = StateManager.GetState(typeof(MenuState));
            }
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            GameClient.World.Save();
            this.Exit();
        }
    }
}
